package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckitemService checkitemService;

    @RequestMapping("/add")
    public Result addCheckItem(@RequestBody CheckItem checkItem) {
        Boolean add = false;
        if (checkItem != null)
            checkitemService.add(checkItem);
        return new Result(true, "添加项目成功");
    }

    @RequestMapping("/del")
    public Result delCheckItem(Integer id) {
        if (id != 0)
            if (checkitemService.findById(id) != null)
                checkitemService.delById(id);
        return new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/update")
    public Result updateCheckItem(@RequestBody CheckItem checkitem) {
        if (checkitem != null)
            checkitemService.edit(checkitem);
        return new Result(true, "修改项目成功");
    }

    @RequestMapping("/find")
    @ResponseBody
    public Result findCheckItem(@RequestBody QueryPageBean queryPageBean) {
        PageResult checkitems = null;
        if (queryPageBean != null)
            checkitems = checkitemService.findByPage(queryPageBean);
        if (checkitems != null)
            return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkitems);
        return new Result(false, MessageConst.QUERY_CHECKITEM_FAIL);
    }



    @RequestMapping("/findByid")
    @ResponseBody
    public Result findCheckId(@RequestBody Integer id) {
            CheckItem checkItem = checkitemService.findById(id);
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);
    }
}
